/*******************************************************************************
 **  Name: rbuff.cpp                                                          **
 **  Description:                                                             **
 **                                                                           **
 **  CDDL: Open Source Initiative (OSI) Approved License                      **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  The contents of this file are subject to the terms of the CDDL:          **
 **  Common Development and Distribution License (the "License").             **
 **  You may not use this file except in compliance with the License.         **
 **                                                                           **
 **  You can obtain a copy of the license at $PROJECT_ROOT/LICENSE            **
 **  or http://www.opensolaris.org/os/licensing.  This code is Open Source    **
 **  and you are free to use it within the confines of the license, even      **
 **  for your own closed-source commercial projects, as long as you follow    **
 **  the terms set forth in the CDDL.                                         **
 **                                                                           **
 **  When distributing Covered Code, include this CDDL HEADER in each         **
 **  file and include the License file at $PROJECT_ROOT/LICENSE.              **
 **  If applicable, add the following below this CDDL HEADER, with the        **
 **  fields enclosed by brackets "[]" replaced with your own identifying      **
 **  information: Portions Copyright [yyyy] [name of copyright owner]         **
 **                                                                           **
 ******************************************************************************/
// $Id: //depot/munix/libmunix/trunk/rbuff.cpp#3 $

// $Id: //depot/munix/libmunix/trunk/rbuff.cpp#3 $

#include "common.h"
#include "rbuff.h"
#include <string.h>

void
RingBuff::CopyBuff(char * dest) const
{
	memcpy(dest, (char*)_buff, DEFAULT_RING_BUFF_SZ);
}


// Push onto the tail
void
RingBuff::Put(unsigned char c) volatile
{
	_buff[_tail++] = c;
	if (_tail >= _sz) _tail = 0;
	++_len;
}

// Pop from the head
unsigned char
RingBuff::Get() volatile
{
	if (_len == 0)
		return 0;

	unsigned char c = _buff[_head++];
	if (_head >= _sz) _head = 0;
	--_len;

	return c;
}

RingBuff::RingBuff(const RingBuff& rb)
{
    delete [] _buff;
    _buff = new volatile uchar[rb.Size()];
    rb.CopyBuff((char*)_buff);
    _head = rb.Head();
    _tail = rb.Tail();
    _sz   = rb.Size();
}

RingBuff::RingBuff(uint8 sz)
{
    _sz = sz;
    _buff = new volatile uchar[_sz];
	_head = _tail = _len = 0;
}
